﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using EtlComm;
using TaskToLineUp;
using ETLDispatcher;
using System.Threading;

namespace ChinaPetroEtlServices
{
    public partial class ServiceEtl : ServiceBase
    {
        IEDTDispatcher TaskDispatcher;

        public ServiceEtl()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            bool createnew;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, "sunsjw@foxmail.com", out createnew);
            if (createnew)
            {
                Debug.WriteLine("China Petro Data Services Running......");
                Thread.Sleep(1000);
            }
            else
            {
                Debug.WriteLine("China Petro Data Services has been running......\r\nProgram will exit after two seconds.");
                Thread.Sleep(2000);
                Environment.Exit(0);
            }
            try
            {
                EtlGlobal.Initialize();
                //获取任务列表
                IList<StationObject> lstSO = TaskQueuer.StationObjectList;
                TaskDispatcher = new EtlDispatcher();
                TaskDispatcher.log = LogFactory.CreateLog<ServiceBaseLog>();
                TaskDispatcher.Active = true;
                //调度任务
                TaskDispatcher.Initialize(lstSO);
                TaskDispatcher.Run();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        protected override void OnStop()
        {
            EtlGlobal.IsTerminated = true;
            TaskDispatcher.Active = false;
            TaskDispatcher.Stop();
            base.OnStop();
        }

        protected override void OnPause()
        {
            base.OnPause();
        }

        protected override void OnContinue()
        {
            base.OnContinue();
        }
    }
}
